package qc.module.cms.api.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qc.common.core.exception.QCPromptException;
import qc.common.core.utils.DateUtil;
import qc.module.cms.dto.securityledger.SecurityLedgerDto;
import qc.module.cms.service.SecurityLedgerService;

import java.util.Date;
import java.util.List;

/**
 * SecurityLedgerController
 *
 * @author QuCheng Tech
 * @since 2023/12/26
 */
@RestController
@RequestMapping("/securityledger")
public class SecurityLedgerController {
    private SecurityLedgerService securityLedgerService;

    @Autowired
    public void setSecurityLedgerService(SecurityLedgerService securityLedgerService) {
        this.securityLedgerService = securityLedgerService;
    }

    /**
     * 查询所有画面信息
     *
     * @return java.util.List<qc.module.cms.dto.ledview.SecurityLedgerDto>
     * @author QuCheng Tech
     * @since 2023/12/26
     */
    @RequestMapping(value = "/query", method = {RequestMethod.GET})
    public List<SecurityLedgerDto> query() throws QCPromptException {
        return securityLedgerService.getAll();
    }

    /**
     * 获取指定日期历史上发生的事故集合
     *
     * @param id 指定的日期，为空时查询当天
     * @return java.util.List<qc.module.cms.dto.securityledger.SecurityLedgerDto>
     * @author QuCheng Tech
     * @since 2023/12/27
     */
    @RequestMapping(value = "/date", method = {RequestMethod.GET})
    public List<SecurityLedgerDto> getByDateByParam(@RequestParam String id) throws QCPromptException {
        return getByDate(id);
    }

    /**
     * 获取指定日期历史上发生的事故集合
     *
     * @param id 指定的日期，为空时查询当天
     * @return java.util.List<qc.module.cms.dto.securityledger.SecurityLedgerDto>
     * @author QuCheng Tech
     * @since 2023/12/27
     */
    @RequestMapping(value = "/date/{id}", method = {RequestMethod.GET})
    public List<SecurityLedgerDto> getByDateByPath(@PathVariable(required = false) String id) throws QCPromptException {
        return getByDate(id);
    }

    List<SecurityLedgerDto> getByDate(String dateString) {
        //日期默认为当天
        Date date = DateUtil.getTodayDate();
        if (!StringUtils.isBlank(dateString)) {
            //如果有输入日期进行转换，转换失败不进行异常处理
            Date d = DateUtil.parseDate(dateString);
            if (d != null)
                date = d;
        }

        return securityLedgerService.getHistoryByDate(date);
    }

    /**
     * 获取指定画面信息
     *
     * @param id 画面ID
     * @return qc.module.cms.dto.ledview.SecurityLedgerDto
     * @author QuCheng Tech
     * @since 2023/12/26
     */
    @RequestMapping(value = "/get", method = {RequestMethod.GET})
    public SecurityLedgerDto getByParam(@RequestParam int id) throws QCPromptException {
        return securityLedgerService.get(id);
    }

    /**
     * 获取指定画面信息
     *
     * @param id 画面ID
     * @return qc.module.cms.dto.ledview.SecurityLedgerDto
     * @author QuCheng Tech
     * @since 2023/12/26
     */
    @RequestMapping(value = "/get/{id}", method = {RequestMethod.GET})
    public SecurityLedgerDto getByPath(@PathVariable(required = true) int id) throws QCPromptException {
        return securityLedgerService.get(id);
    }

    /**
     * 新增
     *
     * @param dto 画面信息
     * @return java.lang.String
     * @author QuCheng Tech
     * @since 2023/12/26
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public String add(@RequestBody SecurityLedgerDto dto) {
        return securityLedgerService.add(dto);
    }

    /**
     * 修改
     *
     * @param dto 画面信息
     * @return java.lang.String
     * @author QuCheng Tech
     * @since 2023/12/26
     */
    @RequestMapping(value = "/update", method = {RequestMethod.PUT, RequestMethod.POST})
    public String update(@RequestBody SecurityLedgerDto dto) {
        return securityLedgerService.update(dto);
    }

    /**
     * 删除
     *
     * @param id 画面ID
     * @return java.lang.String
     * @author QuCheng Tech
     * @since 2023/12/26
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.DELETE})
    public String deleteByParam(@RequestParam int id) {
        return securityLedgerService.delete(id);
    }

    /**
     * 删除
     *
     * @param id 画面ID
     * @return java.lang.String
     * @author QuCheng Tech
     * @since 2023/12/26
     */
    @RequestMapping(value = "/delete/{id}", method = {RequestMethod.DELETE})
    public String deleteByPath(@PathVariable(required = true) int id) {
        return securityLedgerService.delete(id);
    }
}
